<?php
$adm_menu = "1";
set_time_limit(3600);
ini_set('memory_limit', '1024M');

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") header('Location: index.php');

$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
if (!$db_www->isConnected()) {	
	die("Klientų aptarnavimo sistema laikinai neveikia.");
}

include("accounts_filter.php");


	require_once('tcpdf/config/lang/eng.php');
	require_once('tcpdf/tcpdf.php');

	// Extend the TCPDF class to create custom Header and Footer
	class MYPDF extends TCPDF {

		private $hd = "header";
		
		public function setHeaderString($value){
			$this->hd = $value;
		}
		
		//Page header
		public function Header() {        
			$this->writeHTML($this->hd, true, false, true, false, '');
		}
	}
	
		// create new PDF document
		$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

		// set document information
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('Darius Mikėnas');
		$pdf->SetTitle('Saskaita apmokejimui');
		$pdf->SetSubject('Saskaita');
		$pdf->SetKeywords('Saskaita');

		// set default header data
		$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
		$pdf->setHeaderString("");
		$pdf->setPrintHeader(false);
		$pdf->setPrintFooter(false);

		// set default monospaced font
		$pdf->SetDefaultMonospacedFont('dejavusans');

		//set margins
		$pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
		$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

		//set auto page breaks
		$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

		//set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

		//set some language-dependent strings
		$pdf->setLanguageArray($l);

		// ---------------------------------------------------------

		// set font
		//$pdf->AddFont('dejavusans', 'B', 8);
		$pdf->SetFont('dejavusans', '', 8);
		$pdf->setHeaderFont(Array('dejavusans', '', 8));

		$pdf->setJPEGQuality(75);
		$pdf->setFontSubsetting(false);
		// add a page
		$pdf->AddPage();


$opts = array(
  'http' => array('ignore_errors' => true)
);

//Create the stream context
$context = stream_context_create($opts);



if ((isset($accounts) && !empty($accounts)) || (isset($accounts2) && !empty($accounts2))) {
	if (!isset($site_address)) {
		$site_address = "http://localhost";
	}
	foreach ($accounts as $account) {
     error_log("Processing:" . trim($account["I32_DOK_NR"]) . "\n"); 
		$html = file_get_contents($site_address.substr($_SERVER["SCRIPT_NAME"],0,-12)."printaccounts.php?im_kodas=".trim($account['I32_KODAS_KS'])."&dok_nr=" . iconv('windows-1257', 'UTF-8',urlencode(trim($account["I32_DOK_NR"]))."&nopdf=1"), false, $context);
		//echo $html;
		$pdf->writeHTML($html, true, false, true, false, '');
		$pdf->AddPage();
	}
	
	foreach ($accounts2 as $account) {
		if ($account["I06_OP_TIP"] == "52") {
			$cred= "1";
		} else {
			$cred= "0";
		}
     error_log("Processing:" . trim($account["I06_DOK_NR"]) . "\n"); 
	 
	$html = file_get_contents($site_address.substr($_SERVER["SCRIPT_NAME"],0,-12)."printaccounts.php?cred=".$cred."&type=p&im_kodas=".trim($account['i06_kodas_ks'])."&dok_nr=" . iconv('windows-1257', 'UTF-8',urlencode(trim($account["I06_DOK_NR"])))."&nopdf=1&id=" . iconv('windows-1257', 'UTF-8',urlencode(trim($account["I06_KODAS_PO"]))), false, $context);
		//echo $html;
		$pdf->writeHTML($html, true, false, true, false, '');
		$pdf->AddPage();
	}
	//Close and output PDF document
	$pdf->Output('saskaitos.pdf', 'D');
}


?>